function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function restTime(dateBefore, dateAfter) {

  var restDate = dateAfter.getTime() - dateBefore.getTime()
  var days = Math.floor(restDate / (24 * 3600 * 1000))
  var leave1 = restDate % (24 * 3600 * 1000)    
  var hours = Math.floor(leave1 / (3600 * 1000))
  var leave2 = leave1 % (3600 * 1000)       
  var minutes = Math.floor(leave2 / (60 * 1000))
  var leave3 = leave2 % (60 * 1000)      
  var seconds = Math.round(leave3 / 1000)

  minutes = (minutes == 60 ? 0: minutes)
  seconds = (seconds == 60 ? 0 : seconds)
  
  days = (days < 0 ? 0 : days)
  hours = (hours < 0 ? 0 : hours)
  minutes = (minutes < 0 ? 0 : minutes)
  seconds = (seconds < 0 ? 0 : seconds)
  
  return { day: days, hour: formatNumber(hours), minute: formatNumber(minutes), second: formatNumber(seconds)}
}

function formatDate(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  return [year, month, day].map(formatNumber).join('/')
}

function formatDatePoint(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  return [year, month, day].map(formatNumber).join('.')
}

function getDateDiff(diffTime) {
  var time = diffTime.replace(/\-/g, "/");
  return time;
}

module.exports = {
  formatTime: formatTime,
  restTime: restTime,
  formatDate: formatDate,
  getDateDiff: getDateDiff,
  formatDatePoint: formatDatePoint
}
